package data;

import isg3.Persona;
import isg3.Usuario;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


public class JDBCPersonaDAO implements IPersonaDAO{

	
	public String selectPersonaOID( String personaID) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oidp = null;
	        String sql = "SELECT * FROM Persona WHERE (NIF = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, personaID);
	            result = stmt.executeQuery();

	            result.next();
	            oidp = result.getString("OIDpersona");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oidp;

	}

	
	public String select( String personaOID) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
       Persona p = null;
       String usuario=null;
        String sql = "SELECT * FROM Persona WHERE (OIDpersona = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, personaOID);
            result = stmt.executeQuery();

            result.next();
           /* p.setApellidos(result.getString("apellidos"));
            p.setContrasena(result.getString("passwd"));
            p.setCorreo(result.getString("correo"));
            p.setNIF(result.getString("NIF"));*/
            usuario=result.getString("usuario");
            
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return usuario;

	}

	
	public List selectAllPersonas() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Persona";
            stmt = conn.prepareStatement(sql); //conn es una conexión válida
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Persona temp = new Persona();
                temp.setApellidos(result.getString("apellidos"));
                temp.setContrasena(result.getString("passwd"));
                temp.setCorreo(result.getString("correo"));
                temp.setNIF(result.getString("NIF"));
                temp.setUsuario(result.getString("usuario"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;

	}

}
